var i;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var Const_Common = require("AA_Const_Common");
var GongGaoView = require("AA_GongGaoView");
var GravityPlatform = require("AA_GravityPlatform");
var YpNetMag = require("AA_YpNetMag");
var EvenType = require("AA_EvenType");
var PlayerDataManager = require("AA_PlayerDataManager");
var Excel = require("AA_Excel");
var ExcelLoader = require("AA_ExcelLoader");
var GameDataManager = require("AA_GameDataManager");
var GameGlobalVariable = require("AA_GameGlobalVariable");
var UIConfig_Game = require("AA_UIConfig_Game");
var UIConfig_Home = require("AA_UIConfig_Home");
var BundleConfig = require("AA_BundleConfig");
var GameConfig = require("AA_GameConfig");
var AudioManager = require("AA_AudioManager");
var EventManager = require("AA_EventManager");
var HttpManager = require("AA_HttpManager");
var LogManager = require("AA_LogManager");
var ResLoader = require("AA_ResLoader");
var UIManager = require("AA_UIManager");
var UIView = require("AA_UIView");
var CommonUtils = require("AA_CommonUtils");
var _decorator = cc._decorator;
var _ccclass = _decorator.ccclass;
var _property = _decorator.property;

var _def_UI_Entry = function (e) {
  function _creator() {
    var t = null !== e && e.apply(this, arguments) || this;
    t.progress_loading = null;
    t.tipView = null;
    t.gonggaoview = null;
    t.timeId = 0;
    t.isDataOk = false;
    t.isBoundOk = false;
    t.isClose = false;
    t.procount = 0;
    t.maxcount = 0;
    return t;
  }
  __extends(_creator, e);
  _creator.prototype.onLoad = function () {
    cc.director.getCollisionManager().enabled = true;
    this.tipView.active = false;
  };
  _creator.prototype._show = function () {
    return __awaiter(this, undefined, Promise, function () {
      var e;
      var t = this;
      return __generator(this, function () {
        this.progress_loading.progress = 0;
        // if (cc.sys.isNative) {
        YPSDK.init(39, "https://platform-shop-dev.hanyougame.com", {
          gameChannelList: {
            h5: {
              platformType: "h5",
              describe: "默认 H5 渠道",
              version: "1.0.0"
            },
            android: {
              platformType: "h5",
              describe: "默认 H5 渠道安卓",
              version: t
            },
            tt: {
              platformType: "tt",
              appId: "tt09297f94961f881b02",
              describe: "默认 TT 渠道",
              version: "1.0.0"
            },
            wx: {
              platformType: "wx",
              appId: "wx6baaaa27ab5186ff",
              describe: "默认 WX 渠道",
              version: "1.0.0"
            }
          }
        });
        YPSDK.login();

        console.log('YPSDK111111111')
        // }
        e = cc.tween(this).delay(5).call(function () {
          console.error("5s后，仍然未登录成功，直接进入游戏");
          t.startLoadGame();
        }).start();
        YPSDK.setLoginCallBack(function (o) {
          return __awaiter(t, undefined, undefined, function () {
            return __generator(this, function () {
              if (o) {
                GravityPlatform.default.GA_Init(YPSDK.Platform.loginData.bindingId);
                e.stop();
                this.startLoadGame();
              }
              return [2];
            });
          });
        });
        GameGlobalVariable.GameGlobalVariable.openName("Loading");
        return [2];
      });
    });
  };
  _creator.prototype.startLoadGame = function () {
    var e = [];
    e.push(this._loadGameConfig);
    e.push(this._loadRemoteConfig);
    e.push(this._loadExcelData.bind(this));
    e.push(this._loadUserData);
    var t = [];
    t.push(this._loadCommonBundle);
    if (GameGlobalVariable.GameGlobalVariable.getNewGame()) {
      t.push(this._loadGameBundle);
    } else {
      t.push(this._loadMainBundle);
    }
    this.boundLoad(t, this.boundOk);
    this._completeTasks(e, this.dataOk);
    this.maxcount = t.length + e.length;
    cc.sys.platform == cc.sys.WECHAT_GAME && mg.showShareMenu({
      menus: ["shareAppMessage", "shareTimeline"]
    });
  };
  _creator.prototype.hideTip = function () {
    this.tipView.active = false;
  };
  _creator.prototype.openTip = function () {
    this.tipView.active = true;
  };
  _creator.prototype.dataOk = function () {
    this.isDataOk = true;
    this._loadGame();
  };
  _creator.prototype.boundOk = function () {
    this.isBoundOk = true;
    this._loadGame();
  };
  _creator.prototype.boundLoad = function (e, t) {
    var o = this;
    var n = 0;
    var i = Promise.resolve();
    e.forEach(function (t) {
      i = i.then(function () {
        return t();
      }).then(function () {
        o.procount++;
        o._setProgress(0 / e.length);
      }).catch(function () {
        n++;
      });
    });
    return i.then(function () {
      0 == n && t.call(o);
    });
  };
  _creator.prototype._loadExcelData = function () {
    var e = this;
    return new Promise(function (t) {
      var o = new Date().getTime();
      ExcelLoader.ExcelLoader.loadAll().then(function () {
        LogManager.LogMgr.info("游戏数据表加载成功", new Date().getTime() - o, Excel.Excel.shuiguogonggao_all);
        t(true);
        if (cc.sys.localStorage.getItem("isnew")) {
          e.showGongGao();
        } else {
          cc.sys.localStorage.setItem("isnew", "ok");
          e.isClose = true;
        }
      }).catch(function () {
        e.isClose = true;
      });
    });
  };
  _creator.prototype.showGongGao = function () {
    var e = CommonUtils.default.getNewGongGao();
    if (e.length > 0) {
      var t = cc.sys.localStorage.getItem("showgonggao");
      var o = CommonUtils.default.getServerTime(e[0].time) + "";
      if (t && t == o) {
        this.isClose = true;
      } else {
        this.gonggaoview.node.active = true;
        this.gonggaoview.initUi(this.backOn.bind(this));
      }
    }
  };
  _creator.prototype.backOn = function () {
    this.isClose = true;
    var e = CommonUtils.default.getNewGongGao()[0].time;
    cc.sys.localStorage.setItem("showgonggao", CommonUtils.default.getServerTime(e) + "");
    this.isBoundOk && this.isDataOk && this._loadGame();
  };
  _creator.prototype.nowClose = function () {
    var e = CommonUtils.default.getNewGongGao();
    console.log("打印当前公告获取数量", e, Excel.Excel.shuiguogonggao_all);
    return e.length <= 0 || this.isClose;
  };
  _creator.prototype._loadGame = function () {
    if (this.isBoundOk && this.isDataOk && this.nowClose()) {
      clearInterval(this.timeId);
      LogManager.LogMgr.info("开始游戏！");
      this._close();
      if (PlayerDataManager.PlayerDataMgr.GetGuideIndexByTaskName(Const_Common.GuideName.战斗背包) != Const_Common.GameBagGuideIndex.引导完结) {
        EventManager.EventMgr.dispatchEvent(EvenType.EVENT_TYPE.Game_Load_View, true);
        PlayerDataManager.PlayerDataMgr.GMSetGuideIndex(Const_Common.GuideName.战斗背包, Const_Common.GameBagGuideIndex.引导初始1);
        GameDataManager.GameDataMgr.ClearGameBagData();
        GameGlobalVariable.GameGlobalVariable.nowlevel = 1;
        UIManager.default.open(BundleConfig.BundleNames.Game, UIConfig_Game.UIView_Game.UI_GameView);
      } else {
        EventManager.EventMgr.dispatchEvent(EvenType.EVENT_TYPE.Game_Load_View, true);
        UIManager.default.open(BundleConfig.BundleNames.Home, UIConfig_Home.UIView_Home.UI_Hall);
      }
      this.onInitWhiteName();
    }
  };
  _creator.prototype._completeTasks = function (e, t) {
    var o = this;
    var n = 0;
    var i = Promise.resolve();
    e.forEach(function (t) {
      i = i.then(function () {
        return t();
      }).then(function () {
        o.procount++;
        o._setProgress(0 / e.length);
      }).catch(function () {
        n++;
      });
    });
    return i.then(function () {
      0 == n && t.call(o);
    });
  };
  _creator.prototype._loadGameConfig = function () {
    var e = this;
    return new Promise(function (t, o) {
      return __awaiter(e, undefined, undefined, function () {
        var e;
        var n;
        return __generator(this, function (i) {
          switch (i.label) {
            case 0:
              e = new Date().getTime();
              return [4, ResLoader.default.loadRes(BundleConfig.BundleNames.Resouce, "res/config/GameConfig", cc.JsonAsset)];
            case 1:
              if (n = i.sent()) {
                GameConfig.default.appConfig = n.json;
                LogManager.LogMgr.info("Load game config success:", GameConfig.default.appConfig);
                n.decRef();
                t(true);
                LogManager.LogMgr.info("游戏本地配置加载成功", new Date().getTime() - e);
              } else {
                LogManager.LogMgr.info("Load game config failure");
                o(false);
              }
              return [2];
          }
        });
      });
    });
  };
  _creator.prototype._loadRemoteConfig = function () {
    var e = this;
    return new Promise(function (t, o) {
      return __awaiter(e, undefined, undefined, function () {
        var e;
        var n;
        var i;
        var a;
        return __generator(this, function (r) {
          switch (r.label) {
            case 0:
              e = new Date().getTime();
              if (n = GameConfig.default.appConfig.RemoteUrl) {
                i = cc.path.join(n, "ADConfig.json");
                return [4, ResLoader.default.loadRemote(i, {
                  ext: ".json"
                })];
              } else {
                return [3, 2];
              }
            case 1:
              if ((a = r.sent()) && a.json) {
                GravityPlatform.default.videoId = a.json.ADUnitId[0];
                GameConfig.default.adConfig = a.json;
                LogManager.LogMgr.info("Load remote config success:", GameConfig.default.adConfig);
                t(true);
                LogManager.LogMgr.info("远程配置加载成功", new Date().getTime() - e);
              } else {
                LogManager.LogMgr.info("Load remote config failure:", i);
                o(false);
              }
              return [3, 3];
            case 2:
              LogManager.LogMgr.info("Load remote config failure: RemoteUrl is null");
              o(false);
              r.label = 3;
            case 3:
              return [2];
          }
        });
      });
    });
  };
  _creator.prototype._loadCommonBundle = function () {
    var e = this;
    return new Promise(function (t, o) {
      return __awaiter(e, undefined, undefined, function () {
        var e;
        return __generator(this, function () {
          e = new Date().getTime();
          ResLoader.default.loadBundle(BundleConfig.BundleNames.Common).then(function (n) {
            console.log("Loaded main bundle", n);
            if (n) {
              t(true);
              console.log("Common分包下载", new Date().getTime() - e);
            } else {
              o(false);
            }
          });
          return [2];
        });
      });
    });
  };
  _creator.prototype._loadGameBundle = function () {
    var e = this;
    return new Promise(function (t, o) {
      return __awaiter(e, undefined, undefined, function () {
        var e;
        return __generator(this, function () {
          e = new Date().getTime();
          ResLoader.default.loadBundle(BundleConfig.BundleNames.Game).then(function (n) {
            LogManager.LogMgr.info("Loaded main bundle", n);
            if (n) {
              t(true);
              console.log("Game分包下载", new Date().getTime() - e);
            } else {
              o(false);
            }
          });
          return [2];
        });
      });
    });
  };
  _creator.prototype._loadMainBundle = function () {
    var e = this;
    return new Promise(function (t, o) {
      return __awaiter(e, undefined, undefined, function () {
        var e;
        return __generator(this, function () {
          e = new Date().getTime();
          ResLoader.default.loadBundle(BundleConfig.MainGameBundle).then(function (n) {
            console.log("Loaded main bundle", n);
            if (n) {
              t(true);
              console.log("Home分包下载", new Date().getTime() - e);
            } else {
              o(false);
            }
          });
          return [2];
        });
      });
    });
  };
  _creator.prototype._yp_sdk_init = function () {
    var e = this;
    var t = GameConfig.default.appConfig.Version;
    var o = {
      gameChannelList: {
        h5: {
          platformType: "h5",
          describe: "默认 H5 渠道",
          version: t
        },
        android: {
          platformType: "h5",
          describe: "默认 H5 渠道安卓",
          version: t
        },
        tt: {
          platformType: "tt",
          appId: "tt09297f94961f881b02",
          describe: "默认 TT 渠道",
          version: t
        },
        wx: {
          platformType: "wx",
          appId: "wx6baaaa27ab5186ff",
          describe: "默认 WX 渠道",
          version: t
        }
      }
    };
    return new Promise(function (t, n) {
      return __awaiter(e, undefined, undefined, function () {
        var e;
        return __generator(this, function () {
          e = new Date().getTime();
          console.log('YPSDK222222222222', GameConfig.default.appConfig.ServerID, GameConfig.default.appConfig.ServerUrl);
          YPSDK.init(GameConfig.default.appConfig.ServerID, GameConfig.default.appConfig.ServerUrl, o).then(function () {
            t(true);
            console.log("游品服务器初始化", new Date().getTime() - e);
          }).catch(function () {
            n(false);
          });
          return [2];
        });
      });
    });
  };
  _creator.prototype._yp_sdk_login = function () {
    var e = this;
    return new Promise(function (t, o) {
      return __awaiter(e, undefined, undefined, function () {
        return __generator(this, function () {
          YPSDK.login().then(function () {
            t(true);
          }).catch(function () {
            o(false);
          });
          return [2];
        });
      });
    });
  };
  _creator.prototype.onInitWhiteName = function () {
    return __awaiter(this, undefined, undefined, function () {
      var e;
      return __generator(this, function () {
        e = YPSDK.platformUrl + "/User/GetCfgData?userId=" + YPSDK.Platform.loginData.userUid + "&keyId=NoVideo";
        console.log("初始化白名单", YPSDK.platformUrl, YPSDK.Platform.loginData.userUid, e);
        HttpManager.HttpMgr.requestData(function (e) {
          LogManager.LogMgr.debug("---后台请求白名单数据-->>>>>>res------", e);
          e && e.data && e.data.keyData && "true" == e.data.keyData && (PlayerDataManager.PlayerDataMgr.WHITE_NAME_NO_VIDEO = true);
        }, e);
        return [2];
      });
    });
  };
  _creator.prototype._loadUserData = function () {
    var e = this;
    return new Promise(function (t) {
      return __awaiter(e, undefined, undefined, function () {
        var e;
        return __generator(this, function (o) {
          switch (o.label) {
            case 0:
              o.trys.push([0, 2, , 3]);
              console.error("初始化数据" + YPSDK.Common.curChannelData.platformType);
              AudioManager.AudioMgr.init();
              GameGlobalVariable.GameGlobalVariable.initPeiZhi();

              YPSDK.Common.curChannelData.platformType != YPSDK.GamePlatformType.WX && YPSDK.Common.curChannelData.platformType != YPSDK.GamePlatformType.TT || YpNetMag.YpNetMag.failSever(function () {
                YpNetMag.YpNetMag.isFail = true;
                t(true);
                console.error("数据读取失败");
              });
              return [4, YpNetMag.YpNetMag.initServer(YPSDK.Platform.loginData)];
            case 1:
              o.sent();
              return [3, 3];
            case 2:
              e = o.sent();
              LogManager.LogMgr.error("load user data error:", e);
              return [3, 3];
            case 3:
              t(true);
              return [2];
          }
        });
      });
    });
  };
  _creator.prototype._setProgress = function () {
    var e = Math.max(0, Math.min(1, this.procount / this.maxcount));
    this.progress_loading.progress = e;
  };
  __decorate([_property(cc.ProgressBar)], _creator.prototype, "progress_loading", undefined);
  __decorate([_property(cc.Node)], _creator.prototype, "tipView", undefined);
  __decorate([_property(GongGaoView.default)], _creator.prototype, "gonggaoview", undefined);
  return __decorate([_ccclass], _creator);
}(UIView.default);
exports.default = _def_UI_Entry;